<?php
class Core_Business_Stat extends Core_Business_Abstract {

	/**
	 * Return the Global Statistique
	 * @return Array GblStat
	 */
	public static function getGlobalStat(){
		//init
		$glbstat = array();
		try{
			$adapter = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('db');
			$query = new Zend_Db_Select($adapter);

			//Training
			$query->from('training',array('Nb'=>'count(*)'));
			$query->assemble();
			$result = $adapter->query($query);
			$glbstat['NbTraining'] =intval(current(current($result->fetchAll())));
			$query = new Zend_Db_Select($adapter); 
			$query->from('training',array('Nb'=>'count(distinct training.id)'))
				  ->join('trainingcandidate',"trainingcandidate.trainingid = training.id");
		    $result = $adapter->query($query);
			$glbstat['NbTrainingCandidate'] = intval(current(current($result->fetchAll())));
			
			$query = new Zend_Db_Select($adapter); 
			$query->from(array('u'=>"users"),array('u.lastname','nb'=>'count(*)'))
				  ->join(array('t'=>'training'),"t.createdby = u.id")
				  ->group('u.lastname');
			$query->assemble();
			$result = $adapter->query($query);
			$rows = $result->fetchAll(); 
			if(isset($rows)) {
				$listuser = array();
				foreach ($rows as $item) {
					$listuser[]=array("Client"=>$item['lastname'],'nbTraining'=>$item['nb']);
				}
				$glbstat['detailperuser']=$listuser;
			}
		    
			$query = new Zend_Db_Select($adapter); 
			$query->from(array('u'=>"users"),array('nb'=>'count(*)'))
				  ->join(array('r'=>'userrule'),"r.userid = u.id AND r.ruleid = 4");
			$query->assemble();
			$result = $adapter->query($query);
			$glbstat['NbTrainer'] = intval(current(current($result->fetchAll())));
			$query = new Zend_Db_Select($adapter); 
			$query->from(array('u'=>"users"),array('nb'=>'count(*)'))
				  ->join(array('r'=>'userrule'),"r.userid = u.id AND r.ruleid = 3");
			$query->assemble();
			$result = $adapter->query($query);
			$glbstat['NbCustomer'] = intval(current(current($result->fetchAll())));  
		}
		catch (Exception $e ) {}
		return $glbstat;
	}
}